package modules.majiang
{
	/**
	 *
	 * @author warden_feng
	 * 2013-5-6
	 */
	import com.netease.protobuf.UInt64;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import communication.majiang.MajiangMsgEvent;
	
	import modules.GameEvent;
	import modules.ViewManager;
	import modules.load.Load;
	import modules.majiang.data.TableDataModel;
	import modules.majiang.data.TablesDataManager;
	
	import utils.ConversionTable;
	import utils.FloatHandler;
	import utils.ResourceManager;

	public class CashierViewManager extends ViewManager
	{
		/**
		 * 买入数据
		 * 请求显示该界面时传过来的数据
		 * 显示该界面的目的
		 **/
		private var buyInData:Object;

		/**
		 * 购买的币种
		 **/
		private var moneyType:String = "";

		/**
		 * 最小买入
		 **/
		private var minBuyIn:Number = 0;

		/**
		 * 最大买入
		 **/
		private var maxBuyIn:Number = 10;

		public function CashierViewManager()
		{
			init();
		}

		override protected function init():void
		{
			mainUI = Load.getInstance("fla.view.majiang.CashierUI");

			dispatcher.addEventListener(MajiangEvent.SHOW_CASHIER_VIEW, onGameEvent);

			dispatcher.addEventListener(MajiangEvent.CLOSE_CASHIER_VIEW, onGameEvent);
			
			mainUI.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			mainUI.removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}

		protected function onGameEvent(event:GameEvent):void
		{
			switch (event.type)
			{
				case MajiangEvent.SHOW_CASHIER_VIEW:
					buyInData = event.data;
					show();
					break;
				case MajiangEvent.CLOSE_CASHIER_VIEW:
					close();
					break;
			}
		}

		override protected function close():void
		{
			//清空目的
			buyInData = null;

			if (mainUI.parent)
				mainUI.parent.removeChild(mainUI);
		}

		override protected function onAddToStage(event:Event):void
		{
			mainUI.buyInBalanceValue.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

			mainUI.enterBtn.addEventListener(MouseEvent.CLICK, onClick);
			mainUI.cancelBtn.addEventListener(MouseEvent.CLICK, onClick);

		}

		protected function onClick(event:MouseEvent):void
		{
			switch (event.currentTarget)
			{
				case mainUI.enterBtn:
					onConfirmHandler();
					break;
				case mainUI.cancelBtn:
					close();
					break;
			}
		}

		protected function onKeyDown(event:KeyboardEvent):void
		{
			if (event.keyCode == Keyboard.ENTER)
			{
				onConfirmHandler();
			}
		}

		override protected function onRemovedFromStage(event:Event):void
		{
			// TODO Auto Generated method stub
			super.onRemovedFromStage(event);
		}

		override protected function show():void
		{
			UIAllRefer.popLayer.addChild(mainUI);
			
			mainUI.x = (UIAllRefer.stage.stageWidth - mainUI.width)/2;
			mainUI.y = (UIAllRefer.stage.stageHeight - mainUI.height)/2;

			updateView();
		}

		override protected function updateView():void
		{
			var tableId:UInt64 = buyInData.tableId;
			var tableDataModel:TableDataModel = TablesDataManager.tableDataModeDic[tableId.toString()];

			moneyType = tableDataModel.tableDataFromLobby.currency;

			mainUI.moneySignTxt.text = ConversionTable.getMoneySign(moneyType);

			minBuyIn = buyInData.minBuyIn;
			maxBuyIn = buyInData.maxBuyIn;

			mainUI.minBuyInTxt.text = ConversionTable.moneyToString(minBuyIn, moneyType, moneyType) + "(" + ConversionTable.moneyToString(minBuyIn, moneyType, GlobalData.myCurrency) + ")";
			mainUI.maxBuyInTxt.text = ConversionTable.moneyToString(maxBuyIn, moneyType, moneyType) + "(" + ConversionTable.moneyToString(maxBuyIn, moneyType, GlobalData.myCurrency) + ")";

			mainUI.buyInBalanceValue.text = FloatHandler.TwoFormatToString(maxBuyIn);
			mainUI.buyInBalanceValue1.text = ConversionTable.moneyToString(maxBuyIn, moneyType, GlobalData.myCurrency)

			mainUI.totalBlanceTxt.text = "Balance:" + ConversionTable.moneyToString(GlobalData.balance, GlobalData.myCurrency, moneyType) + "(" + ConversionTable.moneyToString(GlobalData.balance, GlobalData.myCurrency, GlobalData.myCurrency) + ")";

//			onClickCheckBox(maxCheck);
//			
//			if (minBuyIn == 0)
//			{
//				minCheckBox.mouseChildren = false;
//				minCheckBox.mouseEnabled = false;
//				onClickCheckBox(maxCheck);
//			}
//			else
//			{
//				minCheckBox.mouseChildren = true;
//				minCheckBox.mouseEnabled = true;
//			}
		}

		/**
		 * 点击确定按钮确定买入
		 **/
		private function onConfirmHandler():void
		{
			var money:Number = Number(mainUI.buyInBalanceValue.text);
			var showInfo:String = "";
			if (money < minBuyIn)
			{
				showInfo = ResourceManager.getInstance().getString('MahjongResource', 'lessThanMinValue');
				dispatcher.dispatchEvent(new GameEvent(GameEvent.SHOW_WARN_INFO, showInfo));
				return;
			}
			if (money > maxBuyIn)
			{
				showInfo = ResourceManager.getInstance().getString('MahjongResource', 'overMaxValue');
				dispatcher.dispatchEvent(new GameEvent(GameEvent.SHOW_WARN_INFO, showInfo));
				return;
			}

			//判断是否为 因坐下而弹出的界面
			if (buyInData != null && buyInData["aim"] == "sitdown")
			{
				//发送坐下请求
				dispatcher.dispatchEvent(new MajiangMsgEvent(MSProto.MSID_SIT_DOWN_REQ.toString(), {tableId: buyInData.tableId, chairId: buyInData.chairId, money: money}));
			}
			else if (money > 0 && buyInData != null)
			{
				dispatcher.dispatchEvent(new MajiangMsgEvent(MSProto.MSID_TOPUP_REQ.toString(), {tableId: buyInData.tableId, money: money}));
			}

			close();
		}
	}
}
